<template>
	<view class="myPay">
		<view class="payOne" @click="switchTo('/pages/home/address?source=1')">
			<view class="" style="display: flex;align-items: center;">
				<view class="pic" style="margin-left: 30upx;">
					<image src="/static/temp/address.png" mode=""></image>
				</view>
				<view class="" style="margin:0upx 0upx 0 30upx;" v-if="defaultAddress">
					<view class="cell">
						<text style="color:#898989;">{{defaultAddress.addressee}}</text><text class="cellColor" style="margin-left: 10upx;">{{defaultAddress.mobile}}</text>
					</view>
					<view class="address">
						<text class="cellColor">{{defaultAddress.province_name}} {{defaultAddress.city_name}} {{defaultAddress.district_name}} {{defaultAddress.address}}</text>
					</view>
				</view>
			</view>

			<view url=''><i class="iconfont icon-ionf-xia" style="color:#b5b5b5;"></i></view>
		</view>
		<view class="paySecond">
			<view class="payMsg">
				<text>交易编号：</text><text class="num">{{out_trade_no}}</text>
			</view>
			<view class="paySerial">
				<text>支付流水号：</text><text class="num">{{out_trade_no}}</text>
			</view>
			<view class="payMsg">
				<text>支付金额：</text><text class="num" style="font-size: 30upx;color:#ff9600;">{{objStr.allMoney}}元</text>
			</view>
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<view class="payWay">
			请选择您的支付方式
		</view>
		<view class="payMethod" v-for="(item,index) in payForList" :key="index">
			<view class="" style="display: flex;align-items: center;">
				<view class="pic">
					<image :src="item.pic" mode=""></image>
				</view>
				<view class="info">
					{{item.desc}}
				</view>
			</view>
			<view class="unicon">
				<image src="/static/temp/se_icon.png" mode="" @click="changePayfor(item,index)" v-if="payForCheck == item.type"></image>
				<image src="/static/temp/unse_icon.png" mode="" @click="changePayfor(item,index)" v-else></image>
			</view>
		</view>
		<!-- #endif -->
		<view :class="['btn',flagBtn? 'btnColor':'']" @click="navTo">
			下一步
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				payForList: [{
					id: 'wxpay',
					name: "微信",
					type: "1",
					pic: require('@/static/temp/weixin.png'),
					desc: "微信支付",
					loading: false
				}],
				payForCheck: "1",
				flagBtn: false,
				objStr: null,
				out_trade_no: '',
				defaultAddress: null
			};
		},
		onShow() {
			this.flagBtn = false;
		},
		onLoad(options) {
			if (options.objStr) {
				this.objStr = JSON.parse(options.objStr);
			}
			this.initData();
		},
		methods: {
			switchTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			initData() {
				let userInfo = uni.getStorageSync('userInfo');
				uni.request({
					url: this.$base_url + '/member/address',
					data: {
						
					},
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
						"token": userInfo.token,
					},
					method: "POST",
					success: (result) => {
						if (result.data.code == 10000) {
							let adsList = result.data.data;
							if (adsList) {
								adsList.forEach((item, index) => {
									if (item.is_default == 1) {
										this.defaultAddress = item;
									}
								})
								if (!this.defaultAddress) {
									this.defaultAddress = adsList[0]
								}
							}
						} else if (result.data.code = 40001) {
							this.$api.msg(result.data.message);
						} else {
							this.$api.msg(result.data.message);
						}
						this.loading = true;
					},
					fali: (res) => {
						console.log(res.data);
					}

				});
				uni.request({
					url: this.$base_url + '/order/giftOrder', //仅为示例，并非真实接口地址。
					data: {
						gid: this.objStr.strgids,
						count: this.objStr.count
					},
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
						"token": userInfo.token,
					},
					method: "POST",
					success: (res) => {
						if (res.data.code === 10000) {
							this.out_trade_no = res.data.data.out_trade_no;

						} else {
							this.$api.msg(res.data.message);
						}
						uni.hideLoading();
					},
					fali: (res) => {
						console.log(res.data);
					}
				});
			},
			changePayfor(item, index) {
				this.payForCheck = item.type;
			},
			weixinPay() {
				console.log("发起支付");
				// #ifdef MP-WEIXIN
				uni.login({
					success: (e) => {
						console.log("login success", e);

						console.log(e.code);
						let userInfo = uni.getStorageSync('userInfo');
						uni.request({

							url: this.$base_url + '/pay/giftPay',
							data: {
								pay_type: this.payForCheck,
								out_trade_no: this.out_trade_no
							},
							header: {
								"Content-Type": "application/x-www-form-urlencoded",
								"token": userInfo.token,
							},
							method: "POST",
							success: (res) => {
								var data = res.data;
								if (data.code === 10000) {
									console.log("得到接口prepay_id", res.data.payment);
									let paymentData = data.data;
									if(!paymentData){
										this.$api.msg(res.data.message);
										setTimeout(() => {
											uni.switchTab({
												url: '/pages/home/home'
											})
										}, 1000)
									}else{
										uni.requestPayment({
											timeStamp: paymentData.timeStamp,
											nonceStr: paymentData.nonceStr,
											package: paymentData.package,
											signType: 'MD5',
											paySign: paymentData.paySign,
											success: (res) => {
												uni.hideLoading();
												
												this.$nextTick(() => {
													uni.navigateTo({
														url: '/pages/paySuccess/paySuccess'
													})
												});
											},
											fail: (res) => {
												
												this.$api.msg("取消支付");
												uni.switchTab({
													url: '/pages/index/index'
												})
											},
											complete: () => {
												this.loading = false;
											}
										})
									}
								} else {

									this.$api.msg(res.data.message);
								}
							},
							fail: (e) => {
								console.log("fail", e);
								this.loading = false;
								this.$api.msg("支付失败,原因为: " + e.errMsg);
							}
						})
					},
					fail: (e) => {
						console.log("fail", e);
						this.loading = false;
						this.$api.msg("支付失败,原因为: " + e.errMsg);
					}
				})
				// #endif
				// #ifndef MP-WEIXIN				
				let userInfo = uni.getStorageSync('userInfo');
				uni.request({
					url: this.$base_url + '/pay/giftPay',
					data: {
						pay_type: this.payForCheck,
						out_trade_no: this.out_trade_no
					},
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
						"token": userInfo.token,
					},
					method: "POST",
					success: (res) => {
						var data = res.data;
						if (data.code === 10000) {
							uni.hideLoading();
							let paymentData = data.data;
							if (!paymentData) {
								this.$api.msg(res.data.message);
								setTimeout(() => {
									uni.switchTab({
										url: '/pages/home/home'
									})
								}, 1000)
							} else {
								location.href = paymentData.mweb_url;
								setTimeout(() => {
									uni.switchTab({
										url: '/pages/home/home'
									})
								},1000)
							}

						} else {
							this.$api.msg(res.data.message);
						}
					},
					fail: (e) => {
						reject('err')
						console.log("fail", e);
						this.loading = false;
						this.$api.msg("支付失败,原因为: " + e.errMsg);
					}
				})
				// #endif
			},
			payAfter() {},
			async navTo() {
				uni.showLoading({
					title: 'loading'
				});
				console.log(this.payForCheck)
				this.flagBtn = true;				
				if (this.payForCheck == '1') {
					this.weixinPay();
				}
			}
		}
	}
</script>

<style lang="scss">
	@import './payOnline.scss'
</style>
